//
// Created by tang on 2021/11/27.
//

#ifndef WAVE_GL_TOOLS_H
#define WAVE_GL_TOOLS_H

#include "glad.h"
#include <cstdio>

#if !defined(NDEBUG)
#define GL_CLEAR_ERRORS() while(glGetError() != GL_NO_ERROR)
#define GL_CHECK(x)                                                 \
    do {                                                            \
        GL_CLEAR_ERRORS();                                          \
        x;                                                          \
        const auto err = glGetError();                              \
        if (err != GL_NO_ERROR) {                                   \
            fprintf(stderr, "OpenGL Error at %s:%d, code: %#X\n",    \
                    __FILE__, __LINE__, err);                       \
        }                                                           \
    } while(0)
#else
#define GL_CHECK(x) x
#endif

class Program {
    GLuint m_prog_;
public:
    explicit Program(const char *prog_file);

    ~Program();

    void Use() const;

    GLint AttribLocation(const char *name) const;

    GLint UniformLocation(const char *name) const;
};

class VBO {
    GLuint m_handle_;
    GLenum m_type_;
    GLenum m_usage_;
public:
    VBO(GLenum type, GLenum usage);
    ~VBO();

    void Bind() const;
    void UnBind() const;
    void Allocate(GLsizeiptr new_size, const void *init_data = nullptr) const;
    void Update(GLintptr offset, const void *data, GLsizeiptr size) const;
};

#endif //WAVE_GL_TOOLS_H
